﻿#pragma strict
import System.IO;  
import Microsoft.Win32;  
import System.Text;
class SVNGToolWindow extends SceneView{
	static var win:SceneView;
	static var window;
	@MenuItem ("Assets/SVN/测试",false,0)
	static function Init(){
		window = GetWindow(SVNGToolWindow);
	}
//	function Show(){
//	}
}
static class SVNTool{
	function GetSvnPath(svntype:String,path:String){
	
	var path1 ="\"C:\\Program Files\\TortoiseSVN\\bin\\TortoiseProc.exe\"" + " /command:" + svntype + " /path:\"" + path + "\" /closeonend:2";
	var path2 ="\""+GetWindowsServiceInstallPath("TortoiseSVN") + "\" /command:" + svntype + " /path:\"" + path + "\" /closeonend:2";  
		return path2;
	}
	@MenuItem ("Assets/SVN/更新选中",false,0)
	function SvnSelectUpdate(){
		SaveScene();
		CreateBat(GetSelectObjPath("update"));
	}
	@MenuItem ("Assets/SVN/更新所有",false,2)
	function SvnUpdateAll(){
		SaveScene();
		CreateBat(GetSvnPath("update",Application.dataPath));
	}
	
	@MenuItem ("Assets/SVN/提交选中",false,0)
	function SvnSelectCommit(){
		SaveScene();
		CreateBat(GetSelectObjPath("commit"));
	}
	@MenuItem ("Assets/SVN/提交所有",false,2)
	function SvnCommitAll(){
		SaveScene();
		CreateBat(GetSvnPath("commit",Application.dataPath));
	}
	@MenuItem ("Assets/SVN/提交快捷/代码",false,2)
	function SvnCommit_Q_Script(){
		SaveScene();
		CreateBat(GetSvnPath("commit",Application.dataPath+"/Script"));
	}
	@MenuItem ("Assets/SVN/提交快捷/预制",false,2)
	function SvnCommit_Q_Resources(){
		SaveScene();
		CreateBat(GetSvnPath("commit",Application.dataPath+"/Resources"));
	}
	function SaveScene(){
		EditorApplication.SaveAssets();
		EditorApplication.SaveScene(EditorApplication.currentScene);
	}
	@MenuItem ("Assets/SVN/",false,1)
	function SvnNothing(){
	}
	
	@MenuItem ("Assets/SVN/还原选中",false,0)
	function SvnSelectRevert(){
		SaveScene();
		CreateBat(GetSelectObjPath("revert"));
	}
	@MenuItem ("Assets/SVN/还原所有",false,2)
	function SvnRevertAll(){
		SaveScene();
		CreateBat(GetSvnPath("revert",Application.dataPath));
	}
	
	@MenuItem ("Assets/SVN/清理选中",false,0)
	function SvnSelectClearUp(){
		SaveScene();
		CreateBat(GetSelectObjPath("cleanup"));
	}
	@MenuItem ("Assets/SVN/清理所有",false,2)
	function SvnClearUpAll(){
		SaveScene();
		CreateBat(GetSvnPath("cleanup",Application.dataPath));
	}
	
	function AUTOUpdate(){
		SvnSelectUpdate();
	}
	function SvnTool(){
		EditorGUILayout.BeginHorizontal("box");
			if(GUILayout.Button("更新"))
				SvnSelectUpdate();
			if(GUILayout.Button("提交"))
				SvnSelectCommit();
			if(GUILayout.Button("还原"))
				SvnSelectRevert();
			GUI.color = Color.red;
			if(GUILayout.Button("清理"))
				SvnSelectClearUp();
			GUI.color = Color.white;
		EditorGUILayout.EndHorizontal();
	}
	function GetSelectObjPath(svntype:String){
		var obj:UnityEngine.Object[];
		obj = Selection.objects;
		var Obj:UnityEngine.Object;
		var path:String;
		for(var i = 0;i < obj.length;i++){
			Obj = obj[i];
			path += GetSvnPath(svntype,AssetDatabase.GetAssetPath(Obj)) + "\n";
		}
		return path;
	}
	function CreateBat(data:String){
		var streamWriter : StreamWriter; 
		var fileInfo : FileInfo = new FileInfo(BatPath());
			streamWriter = fileInfo.CreateText();
			streamWriter.WriteLine(data);  
			streamWriter.Close();   
		AssetDatabase.Refresh();
		CoBat();
	}
	function CoBat(){
		var objbat:UnityEngine.Object;
		objbat = AssetDatabase.LoadAssetAtPath("Assets/Script/Component/Svn/svn.bat",UnityEngine.Object);
		AssetDatabase.OpenAsset(objbat,-1);
	}
	function BatPath(){
		return Application.dataPath +"/Script/Component/Svn/svn.bat";
	}

	public static function GetWindowsServiceInstallPath(ServiceName:String )
	        {
	            var key:String  = "SOFTWARE\\" + ServiceName;
	            var path:String  = Registry.LocalMachine.OpenSubKey(key).GetValue("ProcPath").ToString();
	            return path;
	}

}